package com.tju.ocean_ecology_website.mapper;

import com.tju.ocean_ecology_website.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user")
    List<User> findAll();

    List<User> findAllWithPagination(@Param("role") String role,
                                    @Param("status") Boolean status,
                                    @Param("sortField") String sortField,
                                    @Param("sortOrder") String sortOrder,
                                    @Param("offset") int offset,
                                    @Param("pageSize") int pageSize);

    long countAll(@Param("role") String role, @Param("status") Boolean status);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);

    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(String username);

    @Select("SELECT * FROM user WHERE email = #{email}")
    User findByEmail(String email);

    @Select("SELECT COUNT(*) FROM user WHERE username = #{username}")
    boolean existsByUsername(String username);

    @Select("SELECT COUNT(*) FROM user WHERE email = #{email}")
    boolean existsByEmail(String email);

    @Insert("INSERT INTO user(username, password, name, role, email, phone, avatar, create_time, update_time, status) " +
            "VALUES(#{username}, #{password}, #{name}, #{role}, #{email}, #{phone}, #{avatar}, NOW(), NOW(), #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    @Update("UPDATE user SET username = #{username}, name = #{name}, email = #{email}, " +
            "phone = #{phone}, avatar = #{avatar}, update_time = NOW() WHERE id = #{id}")
    int update(User user);

    @Update("UPDATE user SET password = #{password}, update_time = NOW() WHERE id = #{id}")
    int updatePassword(@Param("id") Long id, @Param("password") String password);

    @Update("UPDATE user SET status = #{status}, update_time = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Boolean status);

    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteById(Long id);
}
